package top.wshape1.easyscan.adapter;

import android.annotation.SuppressLint;
import android.content.pm.ApplicationInfo;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import java.util.List;
import java.util.zip.Inflater;

import top.wshape1.easyscan.R;
import top.wshape1.easyscan.activity.EasyScanActivity;
import top.wshape1.easyscan.data.SettingData;
import top.wshape1.easyscan.entity.CustomRule;

/**
 * @author Wshape1
 * @version 1.0
 * @description
 * @since 2023-06-12
 */
public class CustomRuleAdapter extends BaseAdapter {

    private List<CustomRule> customRuleList;

    private EasyScanActivity mainActivity;

    public CustomRuleAdapter(EasyScanActivity mainActivity, List<CustomRule> customRuleList) {
        this.mainActivity = mainActivity;
        this.customRuleList = customRuleList;
    }

    @Override
    public int getCount() {
        return customRuleList.size();
    }

    @Override
    public CustomRule getItem(int position) {
        return customRuleList.get(position);
    }

    @Override
    public long getItemId(int position) {
        return customRuleList.get(position).getId();
    }

    @SuppressLint({"ViewHolder", "InflateParams"})
    @Override
    public View getView(int position, View convertView, ViewGroup parent) {

        CustomRule customRule = getItem(position);

        View view = View.inflate(mainActivity, R.layout.list_view_custom_rule, null);

        ((TextView) view.findViewById(R.id.tv_list_view_name)).setText(customRule.getClassName());
        ((TextView) view.findViewById(R.id.tv_list_view_description)).setText(customRule.getDescription());
        ((TextView) view.findViewById(R.id.tv_list_view_string)).setText(customRule.getMatchString());

        ApplicationInfo appInfo = mainActivity.getGoalAppInfoBy(customRule.getPackageName());

        ((ImageView) view.findViewById(R.id.iv_list_view_icon)).setImageDrawable(appInfo.loadIcon(mainActivity.getPackageManager()));

        return view;
    }
}
